//
//  CBJRefreshView.swift
//  Weibo
//
//  Created by 崔健 on 16/5/27.
//  Copyright © 2016年 cbj. All rights reserved.
//

import UIKit


enum FlyType : Int{
    
    case toFly
    case willFly
    case flying
}



class CBJRefreshView: UIControl {


    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var arrowImage : UIImageView = UIImageView()
    private var refreshLabel : UILabel = UILabel()
    var activityView : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    private var fatherView : UIScrollView?
    var flyType : FlyType = .toFly{
    
        didSet{
        
            switch flyType {
            case .toFly:
                
                UIView.animateWithDuration(0.25, animations: { 
                    
                    self.arrowImage.transform = CGAffineTransformIdentity
                })
                
                
                refreshLabel.text = "下拉起飞"
            case .willFly:
                UIView.animateWithDuration(0.25, animations: {
                    self.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
                })

                refreshLabel.text = "准备起飞"
                
            case .flying:
                
                fatherView?.contentInset.top += 60
                arrowImage.hidden = true
                activityView.startAnimating()
                refreshLabel.text = "正在起飞"
                // 给观察者对象发送请求
                sendActionsForControlEvents(.ValueChanged)
            }

        }
    }
    
    func setupUI(){
    
        backgroundColor = UIColor.yellowColor()
        arrowImage.image = UIImage.init(named: "tableview_pull_refresh")
        refreshLabel.text = "下拉起飞"
        refreshLabel.sizeToFit()
        arrowImage.sizeToFit()
        
        addSubview(activityView)
        addSubview(refreshLabel)
        addSubview(arrowImage)
        
        refreshLabel.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        arrowImage.snp_makeConstraints { (make) in
            
            make.right.equalTo(refreshLabel.snp_left)
            make.centerY.equalTo(self)
        }
        
        activityView.snp_makeConstraints { (make) in
            
            make.center.equalTo(arrowImage)
        }

    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let offset = fatherView?.contentOffset.y
        if fatherView!.dragging {
            
            if  offset < -124 && flyType == .toFly {
                
                flyType = .willFly
                
            }else if offset > -124 && flyType == .willFly{
                
                flyType = .toFly
            }
            
        }else if flyType == .willFly{
        
            flyType = .flying
        }

    }
    
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        
        super.willMoveToSuperview(newSuperview)
        
        if let myFatherView = newSuperview as? UIScrollView {
            
            fatherView = myFatherView // 记录父视图
            myFatherView.addObserver(self, forKeyPath: "contentOffset", options: .New, context: nil)
        }
    }
    
    deinit {
    
        removeObserver(self, forKeyPath: "contentOffset")
    }
    
}
